<?php
/**
 * Created by PhpStorm.
 * User: Thanh Lam
 * Date: 23/04/2014
 * Time: 22:46
 */
namespace Phalconvn\Content;
class GetContent {
    public function get($string){
        if($string==null)
            return true;
        $string = trim($string);
        $content = file_get_contents("$string");
        $subject = $content;
        if ( strlen(strstr($string,'thegioididong')) > 0 ) {
            //price
            $pattern = '# <label class="gia">(.*)</label>#imsU';
            preg_match_all($pattern,$subject,$matches);
            if(!empty($matches[0])){
                $price = str_replace('.','',$matches[0]);
                $price = str_replace('₫','',$price);
                $price = strip_tags($price[0]);
            }else
                $price = '0';
            $result['price'] = $price;
            $result['sell'] =0;
            //content_sell
            $pattern = '#<ul>(.*)</div>#imsU';
            preg_match_all($pattern,$subject,$matches);
            $result['content_sell'] = $matches[1][0];
            $result['company'] = 'thegioididong';
            //comment
          /*  $pattern = '#<div id="ava_2916784" class="avatar">(.*)<div>#imsU';
            preg_match_all($pattern,$subject,$matches);
            $result['comment'] = $matches[0];*/
            return  $result;
        }
        if ( strlen(strstr($string,'vienthonga')) > 0 ) {
            //price
            $pattern = '#<span class="product-price">(.*)</span>#imsU';
            preg_match_all($pattern,$subject,$matches);
            if(!empty($matches[0][0])){
                $price = strip_tags(str_replace(',','',$matches[0][0]));
                $price = str_replace('Đ','',$price);
                $price = strip_tags($price);
            }else
                $price = '0';
            $result['price'] = $price;
            //sell
            $pattern1 = '#<p class="product-online-price clearfix">Giá mua online <span>(.*)</span>#';
            preg_match_all($pattern1,$subject,$matches);
            if(!empty($matches[0][0])){
                $sell = strip_tags(str_replace(',','',$matches[0][0]));
                $sell = str_replace('Đ','',$sell);
                $sell = str_replace('Giá mua online ','',$sell);
                $sell = strip_tags($sell);
            }else
                $sell = '0';
            $result['sell'] =0;
            if($sell > 0){
                $result['sell'] =$sell;
            }
            //content)sell
            $pattern = '#<div class="product-promo-info" id="pro-scroll">(.*)</div></div>#imsU';
            preg_match_all($pattern,$subject,$matches);
            $result['content_sell'] = '';
            if($matches[0][0]!=null)
                $result['content_sell'] = $matches[0][0];
            $result['company'] = 'vienthonga';
            return $result;
        }
        if ( strlen(strstr($string,'fptshop')) > 0 ) {
            //price
            $pattern = '#<span class="price-product">(.*)</span>#imsU';
            preg_match_all($pattern,$subject,$matches);

            if(!empty($matches[0])){
                $price = str_replace('.','',$matches[0]);
                $price = strip_tags($price[0]);
            }
            else
                $price = '0';
            $result['price'] = trim($price);
            $result['sell'] =0;
            //content)sell
            $pattern = '#<div class="other-infor-product">(.*)</div>#imsU';
            preg_match_all($pattern,$subject,$matches);
            $result['content_sell'] = '';
            if($matches[0][0] !=null )
                $result['content_sell'] =$matches[0][0];
            $result['company'] = 'fptshop';
            return $result;
        }
        if ( strlen(strstr($string,'hnammobile')) > 0 ) {
            //price
            $pattern = '#Cty: (.*)đ&nbsp;</p>#imsU';
            preg_match_all($pattern,$subject,$matches);
            if(!empty($matches[0])){
                $price = str_replace('Cty: ','',$matches[0]);
                $price = str_replace(',','',$price);
                $price = str_replace('đ&nbsp;','',$price);
            }else
                $price = '0';
            $result['sell'] =0;
            $result['price'] = trim(strip_tags($price[0]));
            //content)sell
            $pattern = '#<div style="" id="hnambonusblock"> <p>(.*)</p>#imsU';
            preg_match_all($pattern,$subject,$matches);
            $result['content_sell'] = null;
            if($matches[0][0]!=null)
                $result['content_sell'] = $matches[0][0];
            $result['company'] = 'hnammobile';
            return $result;
        }
        if( strlen(strstr($string,'bachlongmobile')) > 0 ) {
            $price = '0';
            $pattern = '#<div class="price CT">(.*)</div>#imsU';
            preg_match_all($pattern,$subject,$matches);
            $price = strip_tags($matches[0][0]);
            $price = str_replace('.','',$price);
            $price = str_replace('VNĐ','',$price);
            $price = str_replace('Cty:','',$price);
            $price = str_replace('BạchLong:','',$price);
            $price =trim($price);
            if($price >0){
                $result['price'] = $price;
            }else{
                $pattern = '#<div class="price BL">(.*)</div>#imsU';
                preg_match_all($pattern,$subject,$matches);
                $price = strip_tags($matches[0][0]);
                $price = str_replace('.','',$price);
                $price = str_replace('VNĐ','',$price);
                $price = str_replace('Cty:','',$price);
                $price = str_replace('BạchLong:','',$price);
                $price =trim($price);
                $result['price'] = $price;
            }
            $result['sell'] = 0;
            $pattern = '#<div class="info_promotion">(.*)</div></div></div>#imsU';
            preg_match_all($pattern,$subject,$matches);
            $result['content_sell']='';
            if($matches[0][0]!=null)
                $result['content_sell'] = $matches[0][0];
            $result['company'] = 'bachlongmobile';
            return $result;
        }
        if( strlen(strstr($string,'hoanghamobile')) > 0 ) {
            $pattern = '#<li><span>Giá tiền:</span><span class="price" itemprop="price">(.*)</span></li>#imsU';
            preg_match_all($pattern,$subject,$matches);
            if(!empty($matches[1])){
                $price = str_replace(',','',$matches[1]);
                $price =  str_replace('vnđ','',$price);
                $price = trim($price[0]);
            }else
                $price = '0';
            $result['price'] = $price;
            $result['sell'] = 0;
            //content)sell
            $pattern = '#<span>Quà tặng:</span>(.*)</li>#imsU';
            preg_match_all($pattern,$subject,$matches);
            $result['content_sell']='';
            if($matches[0][0]!=null)
                $result['content_sell'] = $matches[0][0];
            $result['company'] = 'hoanghamobile';
            return $result;
        }

        if( strlen(strstr($string,'lazada')) > 0 ) {
            $pattern = '#"special_price":"(.*) VND#imsU';
            preg_match_all($pattern,$subject,$matches);
            if(!empty($matches[1][0])){
                $price = str_replace('.','',$matches[1][0]);
                $price = trim($price);
            }else
                $price = '0';
            $result['price'] = $price;
            $result['sell'] = 0;
            $result['content_sell']='';
            $result['company'] = 'lazada';
            return $result;
        }
        if( strlen(strstr($string,'nguyenkim')) > 0 ) {
            $pattern = '#class="price-num">(.*)</span>#imsU';
            preg_match_all($pattern,$subject,$matches);
            if(!empty($matches[1][0])){
                $price = str_replace('.','',$matches[1][0]);
                $price = trim($price);
            }else
                $price = '0';
            $result['price'] = $price;
            $result['sell'] = 0;
            $result['content_sell']='';
            $pattern = '#<input type="radio" class="radio" (.*)>(.*)</label>#imsU';
            preg_match_all($pattern,$subject,$matches);
            if(is_array($matches)){
                $content = '';
                foreach($matches[2] as $val){
                    $content .= '* '.$val.'<br>';
                }
            }
            $result['content_sell'] = $content;
            $result['company'] = 'nguyenkim';
            return $result;
        }
        if( strlen(strstr($string,'cellphones')) > 0 ) {
            $pattern = '#<span class="price" id="price" >(.*)</span>#imsU';
            preg_match_all($pattern,$subject,$matches);
            if(!empty($matches[1][0])){
                $price = str_replace('.','',$matches[1][0]);
                $price = str_replace('₫','',$price);
                $price =strip_tags($price);
                $price = trim($price);
            }else
                $price = '0';
            $result['sell'] = 0;
            $result['price'] = $price;
            $result['content_sell']='';
            $pattern = '#<div class="khuyenmai-info" >(.*)</div>#imsU';
            preg_match_all($pattern,$subject,$matches);
            if($matches[0][0]!=null)
                $result['content_sell'] = $matches[0][0];
            $result['company'] = 'cellphones';
            return $result;
        }
        if( strlen(strstr($string,'viettelstore')) > 0 ) {
            $pattern = '#<strong class="fcOrange fs15">(.*)</strong>#imsU';
            preg_match_all($pattern,$subject,$matches);
            if(!empty($matches[0][1])){
                $price = str_replace('.','',$matches[0][1]);
                $price = str_replace('đ','',$price);
                $price = trim($price);
                $price =strip_tags($price);
            }else
                $price = '0';
            $result['sell'] = 0;
            $result['price'] = $price;
            $result['content_sell']='';
            $pattern = '# <td class="55x">(.*)</td>#imsU';
            preg_match_all($pattern,$subject,$matches);
            if($matches[0][0]!=null)
                $result['content_sell'] = $matches[0][0];
            $result['company'] = 'viettelstore';
            return $result;
        }
    }
}